package com.action;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.RechargeStation;
import com.service.RechargeStationService;
import com.util.PasswordUtil;
import com.util.ValidateHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


/**
 * <p>
 * 充值点信息表 前端控制器
 * </p>
 *
 * @author ZhangXiaoJun
 * @since 2021-09-01
 */
@RestController
@RequestMapping("/recharge/station")
public class RechargeStationAction {

    @Autowired
    private RechargeStationService rechargeStationService;

    /**
     * 分页查找充值点信息
     * @param name
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/list")
    public ResponseBean list(String name, Integer status, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit){
        IPage<RechargeStation> iPage = rechargeStationService.getByPage(page,limit,name,status);
        return new ResponseBean(0,iPage.getTotal(),"成功", iPage.getRecords());
    }

    /**
     * 新增充值点信息
     * @param rechargeStation
     * @return
     */
    @RequestMapping("/save")
    public ResponseBean save(@RequestBody RechargeStation rechargeStation){
        //非空字段检查

        String name = rechargeStation.getName();
        String phone = rechargeStation.getPhone();
        if(ValidateHelper.isEmptyString(name)){
            return new ResponseBean(500,"充值点名称为空", null);
        }
        try {
            if (rechargeStationService.checkNameRepeat(name, "")) {
                return new ResponseBean(500, "充值点名称重复", null);
            }
            if (rechargeStationService.checkPhoneRepeat(phone, "")) {
                return new ResponseBean(500, "联系电话重复", null);
            }
            Date createTime = DateUtil.date();
            rechargeStation.setCreateTime(createTime);
            rechargeStation.setUpdateTime(createTime);
            rechargeStation.setLoginPwd(PasswordUtil.jiami("123456"));
            if (rechargeStationService.save(rechargeStation)) {
                return new ResponseBean(0, "操作成功", null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseBean(500,"操作失败", null);
    }

    /**
     * 修改充值点信息
     * @param rechargeStation
     * @return
     */
    @RequestMapping("/update")
    public ResponseBean update(@RequestBody RechargeStation rechargeStation){
        //非空字段检查
        String name = rechargeStation.getName();
        String id = rechargeStation.getId();
        String phone = rechargeStation.getPhone();
        if(ValidateHelper.isEmptyString(id) || ValidateHelper.isEmptyString(name)){
            return new ResponseBean(500,"充值点名称为空", null);
        }
        try {
            if (rechargeStationService.checkNameRepeat(name,id)) {
                return new ResponseBean(500, "充值点名称重复", null);
            }
            if (rechargeStationService.checkPhoneRepeat(phone, id)) {
                return new ResponseBean(500, "联系电话重复", null);
            }
            Date createTime = DateUtil.date();
            rechargeStation.setUpdateTime(createTime); //修改更新时间
            if (rechargeStationService.updateById(rechargeStation)) {
                return new ResponseBean(0, "操作成功", null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseBean(500,"操作失败", null);
    }

    /**
     * 删除充值点信息，实际上是修改状态值为无效
     * @param id 充值点ID
     * @return
     */
    @RequestMapping("/delete")
    public ResponseBean delete(String id){
        if(ValidateHelper.isEmptyString(id)){
            return new ResponseBean(500,"缺少必填参数", null);
        }
        try {
            RechargeStation rechargeStation = rechargeStationService.getById(id);
            if (null == rechargeStation) {
                return new ResponseBean(500, "未找到充值点信息", null);
            }
            if(rechargeStation.getStatus()==0){
                return new ResponseBean(500, "充值点已被删除", null);
            }
            Date createTime = DateUtil.date();
            rechargeStation.setUpdateTime(createTime); //修改更新时间
            rechargeStation.setStatus(0); //修改状态为无效
            if (rechargeStationService.updateById(rechargeStation)) {
                return new ResponseBean(0, "操作成功", null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseBean(500,"操作失败", null);
    }

    //重置密码
    @RequestMapping("/resetPwd")
    public ResponseBean resetPwd(String id){
        if(ValidateHelper.isEmptyString(id)){
            return new ResponseBean(500,"缺少必填参数", null);
        }
        try {
            RechargeStation rechargeStation = rechargeStationService.getById(id);
            if (null == rechargeStation) {
                return new ResponseBean(500, "未找到充值点信息", null);
            }
            if(rechargeStation.getStatus()==0){
                return new ResponseBean(500, "充值点已被删除", null);
            }
            Date createTime = DateUtil.date();
            rechargeStation.setUpdateTime(createTime); //修改更新时间
            rechargeStation.setLoginPwd(PasswordUtil.jiami("123456")); //重置密码
            if (rechargeStationService.updateById(rechargeStation)) {
                return new ResponseBean(0, "操作成功", null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ResponseBean(500,"操作失败", null);
    }

}
